package com.yanqu.road.server.cmd;

import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.message.YanQuMessage;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public abstract class CrossCommand implements Command {

	protected static Logger log = LogManager.getLogger(CrossCommand.class.getName());
	private static Logger cmdLogger = LogManager.getLogger("COMMAND_TIME");
	protected long serverId;
	protected int ret;

	@Override
	public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
		serverId = CrossAgentLinkedControl.getLinkedClient(ctx).getServerId();
	//	Date start = new Date();

		try {
			if (parseData(packet)) {
				execute(packet);
				sendData();
			}
		} catch (Exception e) {
			log.error("Cmd error, userId = " + packet.getPlayerId() + ", code = " + packet.getCode(), e);
		} finally {
		/*	Date endDate = new Date();
			long costTime = endDate.getTime() - start.getTime();
			if (costTime > 10) {
				cmdLogger.debug("exec cmd cost time {} Millis, userId {}, protocol Num:{} class:{}  ", costTime,
						packet.getPlayerId(), packet.getCode(), getClass().getSimpleName());
			}*/
		}
	}

	public abstract boolean parseData(YanQuMessage packet) throws Exception;

	public abstract void execute(YanQuMessage packet) throws Exception;

	public abstract void sendData();

}
